﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using com.lowagie.text;
using com.lowagie.text.rtf;
using GDI = System.Drawing;
using CMS.Core.Domain;
using CMS.Data;

namespace CMS.Web.Reports
{
    public partial class LookupReportView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            // Get params
            String type = Request["type"];
            String txt1 = Request["txt1"];
            String txt2 = Request["txt2"];
            String txt3 = Request["txt3"];
            String txt4 = Request["txt4"];

            String txtNgay1 = Request["txtNgay1"];
            String txtNgay2 = Request["txtNgay2"];

            String txtMinSoThuLy = Request["txtMinSoThuLy"];
            String txtMaxSoThuLy = Request["txtMaxSoThuLy"];

            String cmbLinhVuc = Request["cmbLinhVuc"];
            String cmbLoaiDon = Request["cmbLoaiDon"];
            String cmbDiaBan = Request["cmbDiaBan"];

            int total;
            IList list;

            total = NHibernateDaoFactory.Instance.GetDonThuDao().TotalSearchDonThus(type, txt1, txt2, txt3, 
                    txt4, txtNgay1, txtNgay2, Convert.ToInt32(txtMinSoThuLy), Convert.ToInt32(txtMinSoThuLy),
                    Convert.ToInt32(cmbLinhVuc), Convert.ToInt32(cmbLoaiDon), cmbDiaBan);
            list = NHibernateDaoFactory.Instance.GetDonThuDao().SearchDonThus(type, txt1, txt2, txt3,
                    txt4, txtNgay1, txtNgay2, Convert.ToInt32(txtMinSoThuLy), Convert.ToInt32(txtMinSoThuLy),
                    Convert.ToInt32(cmbLinhVuc), Convert.ToInt32(cmbLoaiDon), cmbDiaBan, 0, total); 

            MemoryStream m = new MemoryStream();
            try
            {
                Document document = new Document(PageSize.A4.rotate());
                Response.Clear();

                // Clear the content of the response
                Response.ClearContent();
                Response.ClearHeaders();

                Response.AddHeader("Content-Disposition", "attachment; filename=" + "TraCuuDonThu.rtf");
                Response.ContentType = "application/rtf";
                RtfWriter writer = RtfWriter.getInstance(document, m);
                writer.closeStream = false;
                document.open();
                document.setMargins(40, 30, 30, 30);
                Table table = new Table(3);
                table.setBorder(Rectangle.NO_BORDER);
                //table.setBorderWidth(0);
                table.setCellspacing(0);
                table.setCellpadding(0);
                //table.setAutoFillEmptyCells(true);
                table.setAlignment(Element.__Value.ALIGN_LEFT);
                table.setWidth(100);
                table.setWidths(new float[] { 38, 52, 10 });

                Paragraph pgraph = new Paragraph("UBND QUẬN HÀ ĐÔNG", new Font(Font.TIMES_ROMAN, 13, Font.NORMAL));
                pgraph.setAlignment(Element.__Value.ALIGN_CENTER);
                Cell cell = new Cell(pgraph);
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                pgraph = new Paragraph("CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM", new Font(Font.TIMES_ROMAN, 13, Font.BOLD));
                pgraph.setAlignment(Element.__Value.ALIGN_CENTER);
                cell = new Cell(pgraph);
                cell.setColspan(2);
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                pgraph = new Paragraph(" ", new Font(Font.TIMES_ROMAN, 13, Font.BOLD | Font.UNDERLINE));
                pgraph.setAlignment(Element.__Value.ALIGN_CENTER);
                cell = new Cell(pgraph);
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                pgraph = new Paragraph("Độc lập - Tự do - Hạnh phúc", new Font(Font.TIMES_ROMAN, 13, Font.BOLD | Font.UNDERLINE));
                pgraph.setAlignment(1);
                cell = new Cell(pgraph);
                cell.setColspan(2);
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                pgraph = new Paragraph(" ", new Font(Font.TIMES_ROMAN, 13, Font.NORMAL));
                pgraph.setAlignment(Element.__Value.ALIGN_CENTER);
                cell = new Cell(pgraph);
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                pgraph = new Paragraph("Hà Đông, ngày " + DateTime.Now.ToString("dd") + " tháng " + DateTime.Now.ToString("MM") + " năm " + DateTime.Now.ToString("yyyy") + new String(Char.Parse(" "), 10), new Font(Font.TIMES_ROMAN, 12, Font.NORMAL | Font.ITALIC));
                pgraph.setAlignment(Element.__Value.ALIGN_RIGHT);
                cell = new Cell(pgraph);
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);
                pgraph = new Paragraph(" ", new Font(Font.TIMES_ROMAN, 12, Font.NORMAL | Font.ITALIC));
                pgraph.setAlignment(Element.__Value.ALIGN_RIGHT);
                cell = new Cell(pgraph);
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);
                document.add(table);

                pgraph = new Paragraph("\n\nKẾT QUẢ TRA CỨU ĐƠN THƯ\n", new Font(Font.TIMES_ROMAN, 14, Font.BOLD));
                pgraph.setAlignment(Element.__Value.ALIGN_CENTER);
                document.add(pgraph);


                table = new Table(13);
                table.setBorder(Rectangle.BOX);
                table.setBorderWidth(2);
                table.setCellspacing(5);
                //table.setCellpadding(2);
                table.setAutoFillEmptyCells(true);
                table.setAlignment(Element.__Value.ALIGN_LEFT);
                table.setDefaultHorizontalAlignment(Element.__Value.ALIGN_CENTER);
                table.setDefaultVerticalAlignment(Element.__Value.ALIGN_TOP);
                table.setWidth(100);
                //table.setWidths(new float[] { 7, 15, 15, 10, 15, 15 });

                pgraph = new Paragraph("Số thụ lý", new Font(Font.TIMES_ROMAN, 12, Font.BOLD));
                Cell cHeader = new Cell(pgraph);
                cHeader.setHeader(true);
                table.addCell(cHeader);
                pgraph = new Paragraph("Người viết đơn", new Font(Font.TIMES_ROMAN, 12, Font.BOLD));
                cHeader = new Cell(pgraph);
                cHeader.setHeader(true);
                table.addCell(cHeader);
                pgraph = new Paragraph("Địa chỉ", new Font(Font.TIMES_ROMAN, 12, Font.BOLD));
                cHeader = new Cell(pgraph);
                cHeader.setHeader(true);
                table.addCell(cHeader);
                pgraph = new Paragraph("Ngày viết đơn", new Font(Font.TIMES_ROMAN, 12, Font.BOLD));
                cHeader = new Cell(pgraph);
                cHeader.setHeader(true);
                table.addCell(cHeader);
                pgraph = new Paragraph("Loại đơn", new Font(Font.TIMES_ROMAN, 12, Font.BOLD));
                cHeader = new Cell(pgraph);
                cHeader.setHeader(true);
                table.addCell(cHeader);
                pgraph = new Paragraph("Lĩnh vực", new Font(Font.TIMES_ROMAN, 12, Font.BOLD));
                cHeader = new Cell(pgraph);
                cHeader.setHeader(true);
                table.addCell(cHeader);

                //String strSQL = @"SELECT dt.ID, dt.SoThuLy, dt.NguoiGuiLookup.HoTen,dt.NguoiGuiLookup.DiaChi, dt.DonDeNgay,dt.LoaiDon.TenLoaiDon, dt.LinhVuc.TenLinhVuc FROM DonThu dt WHERE ";

                int i = 0;
                foreach (Object[] item in list)
                {
                    i++;
                    string y = item[1] != null ? ((DateTime)item[1]).ToString("yyyy") : "";

                    pgraph = new Paragraph(item[14] == null ? "" : (item[14].ToString() + "/" + y), new Font(Font.TIMES_ROMAN, 12, Font.NORMAL));
                    cell = new Cell(pgraph);
                    table.addCell(cell);

                    pgraph = new Paragraph(item[3] == null ? "" : item[3].ToString(), new Font(Font.TIMES_ROMAN, 12, Font.NORMAL));
                    cell = new Cell(pgraph);
                    cell.setHorizontalAlignment(Element.__Value.ALIGN_LEFT);
                    table.addCell(cell);

                    pgraph = new Paragraph(item[4] == null ? "" : item[4].ToString(), new Font(Font.TIMES_ROMAN, 12, Font.NORMAL));
                    cell = new Cell(pgraph);
                    cell.setHorizontalAlignment(Element.__Value.ALIGN_LEFT);
                    table.addCell(cell);

                    pgraph = new Paragraph((item[1] != null) ? ((DateTime)item[1]).ToString("dd/MM/yyyy") : "", new Font(Font.TIMES_ROMAN, 12, Font.NORMAL));
                    cell = new Cell(pgraph);
                    table.addCell(cell);

                    pgraph = new Paragraph(item[15] == null ? "" : item[15].ToString(), new Font(Font.TIMES_ROMAN, 12, Font.NORMAL));
                    cell = new Cell(pgraph);
                    cell.setHorizontalAlignment(Element.__Value.ALIGN_LEFT);
                    table.addCell(cell);

                    pgraph = new Paragraph(item[16] == null ? "" : item[16].ToString(), new Font(Font.TIMES_ROMAN, 12, Font.NORMAL));
                    cell = new Cell(pgraph);
                    cell.setHorizontalAlignment(Element.__Value.ALIGN_LEFT);
                    table.addCell(cell);                    
                }

                document.add(table);
                document.close();
            }
            catch (Exception ex)
            {

            }

            Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
            m.Close();
        }

    }
}
